package com.kuaimacode.kframework.admin.config.freemaker;

import com.kuaimacode.kframework.util.DictUtil;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

import java.util.List;
/**
* Created by lwj on 2019-03-19.
*/
public class ConvertTemplate implements TemplateMethodModelEx {

    /**
     *
     * @param lable 字典标识
     * @param key  键值对标识
     * @return
     * @throws TemplateModelException
     */
    @Override
    public Object exec(List arguments) {
        if (null != arguments && 2 == arguments.size()) {
            String lable = String.valueOf(arguments.get(0));
            String key = String.valueOf(arguments.get(1));
            if(lable == null || key == null){
                return null;
            }else{
                return DictUtil.keyValue(lable, key);
            }
        } else if (null != arguments && 3 == arguments.size()){//根据父类 label and code ,子类型code 获取子类型 value
            String lable = String.valueOf(arguments.get(0));
            String p_code = String.valueOf(arguments.get(1));
            String code = String.valueOf(arguments.get(2));
            if(lable == null ||  p_code == null ||code==null){
                return null;
            }else{
                return DictUtil.keyValueByPCode(lable, p_code,code);
            }
        }else {

            return null;
        }
    }
}
